var myApp = new Framework7({
    init: false,
    router: true,
    animateNavBackIcon: true,
    swipePanel: 'left',
    precompileTemplates: true,
    template7Pages: true
});
var mainView = null;
var $ = jQuery = Dom7;
var app = {
    api  : '/api/',
    md5_salt : '48024573fc3c5a35651c255a5daa43e9'
}; 
app.success = function ( message) {
    myApp.addNotification({
        title:'成功提示',
        message: message
    });
}
app.error = function ( message) {
    myApp.addNotification({
        title:'错误提示',
        message: message
    });
}

//窗口加载完成事件
window.onload = function(){
    bridge.ready();
}

app.initApp = function (page) {
    mainView = myApp.addView('.view-main', {
        dynamicNavbar: false,
        animatePages: false,
        swipeBackPage: false,
        reloadPages: true,
        domCache: true,
        preloadPreviousPage: false
    });

    myApp.init();
    if(typeof page != 'undefined') {
        mainView.router.load({
            pageName: page
        });
    }
    app.initPages();
}

app.initPages = function () {
    bridge.doScan('#scanBar', function(data) {
        alert(data)
    });

    $('#home').on('tab:show', function () {
        $('#mainTitle').html('首页');
    });

    $('#contacts').on('tab:show', function () {
        $('#mainTitle').html('联系人');
    });

    $('#settings').on('tab:show', function () {
        $('#mainTitle').html('设置');
    });

}


$('#loginForm').on('submit', function (event) {
    doLogin();
    event.preventDefault();
    return false;
});
$('#loginBtn').click(function (event) {
    doLogin();
    event.preventDefault();
    return false;
});

//登录操作
function doLogin() {
    var data = {};
    data.account = $('#l_account').val();
    data.password = $.md5($('#l_pass').val() + app.md5_salt);
    bridge.store('account', data.account);
    bridge.store('password', data.password);
    bridge.getToken(data, function(res){
        bridge.initApp(function(){
            app.initApp('index');
        });
    });
}